package com.example.pkmedical.Activity.rollovershock;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.Fragments.NeedResumeActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.FinishActivityManager;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.example.pkmedical.Utils.CommandUtil.RALL_FOUR;
import static com.example.pkmedical.Utils.CommandUtil.RALL_ONE;
import static com.example.pkmedical.Utils.CommandUtil.RALL_THREE;
import static com.example.pkmedical.Utils.CommandUtil.RALL_TWO;

public class RolloverShockCantStartActivity extends BaseActivity {

    @BindView(R.id.rollover_error_image)
    ImageView rolloverErrorImage;
    @BindView(R.id.rollover_error_button)
    Button rolloverErrorButton;
    @BindView(R.id.rollover_error_image_hl)
    ImageView rolloverErrorImageHl;
    byte[] mBytes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rollovershock_error);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        FinishActivityManager.getManager().addActivity(this);
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
    }

    //RALL_ONE//右下
    //RALL_TWO//左下
    //RALL_THREE//左上
    //RALL_FOUR//右上
    @Override
    public void initView() {
        Log.d("yangshuo", CommandUtil.BED_FRONT + "---" + CommandUtil.BED_AFTER);
        if (CommandUtil.BED_FRONT <= 3 && CommandUtil.BED_AFTER == 0) {//床体平放
            rolloverErrorImageHl.setVisibility(View.GONE);
            if (RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//0000
                rolloverErrorImageHl.setVisibility(View.GONE);
            } else if (RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//000X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_righttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//00X0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//00XX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_twotop);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//0X00
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//0X0X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown_righttop);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//0XX0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//0XXX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall_righttop);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//X000
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_rightdown);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//X00X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_rightall);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//X0X0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop_rightdown);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//X0XX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop_rightall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//XX00
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_twodown);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//XX0X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown_rightall);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//XXX0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall_rightdown);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//XXXX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_all);
            }
        } else if (CommandUtil.BED_FRONT >= 40 && CommandUtil.BED_AFTER == 0) {//后背大于40度腿为0度
            rolloverErrorImageHl.setVisibility(View.VISIBLE);
            rolloverErrorImage.setImageResource(R.drawable.img_back_up);
            if (RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//0000
                rolloverErrorImageHl.setVisibility(View.GONE);
            } else if (RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//000X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_right_top);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//00X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//00XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_top_all);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//0X00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//0X0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_righttop_leftdown);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//0XX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//0XXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_righttop);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//X000
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightdown);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//X00X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightall);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//X0X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftup_rightdown);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//X0XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop_rightall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//XX00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_downall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//XX0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown_rightall);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//XXX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_rightdown);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//XXXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_all);
            }
        } else if (CommandUtil.BED_FRONT >= 40 && CommandUtil.BED_AFTER >= 25) {//后背大于40度腿大于20度
            rolloverErrorImageHl.setVisibility(View.VISIBLE);
            rolloverErrorImage.setImageResource(R.drawable.img_back_leg_up);
            if (RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//0000
                rolloverErrorImageHl.setVisibility(View.GONE);
            } else if (RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//000X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_right_top);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//00X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//00XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_top_all);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//0X00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//0X0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_righttop_leftdown);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//0XX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//0XXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_righttop);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//X000
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightdown);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//X00X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightall);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//X0X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftup_rightdown);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//X0XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop_rightall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//XX00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_downall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//XX0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown_rightall);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//XXX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_rightdown);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//XXXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_all);
            }
        } else if (CommandUtil.BED_FRONT == 0 && CommandUtil.BED_AFTER >= 25) {//后背0度腿大于25度
            rolloverErrorImageHl.setVisibility(View.VISIBLE);
            rolloverErrorImage.setImageResource(R.drawable.img_leg_up);
            if (RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//0000
                rolloverErrorImageHl.setVisibility(View.GONE);
            } else if (RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//000X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_right_top);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//00X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//00XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_top_all);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//0X00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//0X0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_righttop_leftdown);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//0XX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//0XXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_righttop);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//X000
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightdown);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//X00X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_rightall);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//X0X0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftup_rightdown);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//X0XX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_lefttop_rightall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//XX00
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_downall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//XX0X
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftdown_rightall);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//XXX0
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_leftall_rightdown);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//XXXX
                rolloverErrorImageHl.setImageResource(R.drawable.img_upbed_all);
            }
        } else {
            rolloverErrorImageHl.setVisibility(View.GONE);
            if (RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//0000
                rolloverErrorImageHl.setVisibility(View.GONE);
            } else if (RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//000X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_righttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//00X0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop);
            } else if (RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//00XX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_twotop);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//0X00
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown);
            } else if (RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//0X0X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown_righttop);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//0XX0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall);
            } else if (RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//0XXX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall_righttop);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && RALL_FOUR) {//X000
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_rightdown);
            } else if (!RALL_ONE && RALL_TWO && RALL_THREE && !RALL_FOUR) {//X00X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_rightall);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && RALL_FOUR) {//X0X0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop_rightdown);
            } else if (!RALL_ONE && RALL_TWO && !RALL_THREE && !RALL_FOUR) {//X0XX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_lefttop_rightall);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && RALL_FOUR) {//XX00
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_twodown);
            } else if (!RALL_ONE && !RALL_TWO && RALL_THREE && !RALL_FOUR) {//XX0X
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftdown_rightall);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && RALL_FOUR) {//XXX0
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_leftall_rightdown);
            } else if (!RALL_ONE && !RALL_TWO && !RALL_THREE && !RALL_FOUR) {//XXXX
                rolloverErrorImage.setImageResource(R.drawable.img_pingbed_all);
            }
        }


    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        mBytes = event.getBytes();
        if (event.getCode() == MessageWrap.RAIL_STATE) {
            distinguish(mBytes);
        }
    }

    /**
     * 返回数据识别0:放下，1：抬起
     */
    private void distinguish(byte[] bytes) {
        if (DistinguishUtil.distinguishUtil(bytes)) {
            switch (bytes[3]) {
                //护栏
                case 0:
                    switch (bytes[4]) {
                        case 0:
                            CommandUtil.RALL_ONE = false;
                            Log.d("yangshuohulan", "第一个放下");
                            break;
                        case 1:
                            CommandUtil.RALL_ONE = true;
                            Log.d("yangshuohulan", "第一个抬起");
                            break;
                    }
                    switch (bytes[5]) {
                        case 0:
                            CommandUtil.RALL_TWO = false;
                            Log.d("yangshuohulan", "第二个放下");
                            break;
                        case 1:
                            CommandUtil.RALL_TWO = true;
                            Log.d("yangshuohulan", "第二个抬起");
                            break;
                    }
                    switch (bytes[6]) {
                        case 0:
                            CommandUtil.RALL_THREE = false;
                            Log.d("yangshuohulan", "第三个放下");
                            break;
                        case 1:
                            CommandUtil.RALL_THREE = true;
                            Log.d("yangshuohulan", "第三个抬起");
                            break;
                    }
                    switch (bytes[7]) {
                        case 0:
                            CommandUtil.RALL_FOUR = false;
                            Log.d("yangshuohulan", "第四个放下");
                            break;
                        case 1:
                            CommandUtil.RALL_FOUR = true;
                            Log.d("yangshuohulan", "第四个抬起");
                            break;

                    }
                    retrievalType();
                    break;
            }
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick(R.id.rollover_error_button)
    public void onViewClicked() {
        if (CommandUtil.IS_LEFT_RIGHT_ANOTHER){
            SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.CHANGGUI_TTYPE);
        }
        RolloverShockCantStartActivity.this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 检索后背护栏状态
     */
    private void retrievalType() {
        if (CommandUtil.RALL_ONE && CommandUtil.RALL_TWO && CommandUtil.RALL_THREE && CommandUtil.RALL_FOUR && CommandUtil.BED_FRONT < 40 && CommandUtil.BED_AFTER < 25) {
            if (CommandUtil.IS_ROLLOVEW_SHOCK) {
                startActivity(new Intent(MyApp.getAppContext(), NeedResumeActivity.class));
            }if (CommandUtil.IS_LEFT_RIGHT_ANOTHER){
                SerialProtUtil.getInstance().sendBytes300(2, CommandUtil.CHANGGUI_TTYPE);
            }
            RolloverShockCantStartActivity.this.finish();
        } else {
            initView();
            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
        }
    }
}
